<template>
  <div class="app-container">
    <!-- 组件1 -->
    <CargoownerForm :queryParams="queryParams" :showSearch="showSearch" @resetQuery="resetQuery"
      @handleQuery="handleQuery" />


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['cargoowner:owner:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['cargoowner:owner:edit']">刷新</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['cargoowner:owner:remove']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['cargoowner:owner:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 组件2 -->
    <CargoownerList @handleSelectionChange="handleSelectionChange" @handleUpdate="handleUpdate"
      @handleDelete="handleDelete" :ownerList="ownerList" :loading="loading" />


    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改货主对话框 -->
    <!-- 组件3 -->
    <CargoownerSave @submitFormBox="submitForm" @cancelBox="cancel" :formInfo="form" :title="title" :open="open" />

  </div>
</template>

<script setup name="Owner">
import { listOwner, getOwner, delOwner, addOwner, updateOwner } from "@/api/basic/owner";
import CargoownerSave from "@/components/Cargoowner/cargoownerSave.vue";
import CargoownerList from "@/components/Cargoowner/cargoowenerList.vue";
import CargoownerForm from "@/components/Cargoowner/cargoowenerForm.vue";

const { proxy } = getCurrentInstance();

const ownerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,

  },
  rules: {
    name: [
      { required: true, message: "货主名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询货主列表 */
function getList(queryData) {
  loading.value = true;
  listOwner(queryData).then(response => {
    ownerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    city: null,
    personInCharge: null,
    address: null,
    phone: null,
    status: null,
    isDelete: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  };
  proxy.resetForm("ownerRef");
}

/** 搜索按钮操作 */
function handleQuery(queryParams) {
  queryParams.value.pageNum = 1;
  getList(queryParams.value);
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加货主信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOwner(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改货主信息";
  });
}


/** 提交按钮 */
function submitForm(dataForm) {
  if (dataForm.id != null) {
    updateOwner(dataForm).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  } else {
    addOwner(dataForm).then(response => {
      proxy.$modal.msgSuccess("新增成功");
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除货主编号为"' + _ids + '"的数据项？').then(function () {
    return delOwner(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('cargoowner/owner/export', {
    ...queryParams.value
  }, `owner_${new Date().getTime()}.xlsx`)
}

getList();
</script>
